import cv2
import asyncio
import websockets

async def send_video():
    uri = "ws://127.0.0.1:8000/ws"
    # uri = "ws://107.174.254.39:8000/ws"

    async with websockets.connect(
        uri,
        ping_interval=None,   # 放宽心跳间隔；如需关闭设为 None
        ping_timeout=30,    # 放宽心跳超时
        close_timeout=5
    ) as websocket:
        cap = cv2.VideoCapture(0)
        while True:
            ret, frame = cap.read()
            if not ret:
                break
            # 压缩为 JPEG
            ret, jpeg = cv2.imencode('.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 10])
            await websocket.send(jpeg.tobytes())
        cap.release()

asyncio.run(send_video())
